package vip.xiaonuo.smzq.modular.tkq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import oshi.driver.mac.net.NetStat;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb08Xzqh;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb08XzqhMapper;
import vip.xiaonuo.smzq.modular.tkq.entity.TkqKcxmdj;
import vip.xiaonuo.smzq.modular.tkq.entity.vo.TkqVo;
import vip.xiaonuo.smzq.modular.tkq.mapper.TkqKcxmdjMapper;
import vip.xiaonuo.smzq.modular.tkq.service.ITkqKcxmdjService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 探矿权-勘查项目登记 服务实现类
 *
 * @author sqq
 * @since 2024-05-23
 */
@Service
public class TkqKcxmdjServiceImpl extends ServiceImpl<TkqKcxmdjMapper, TkqKcxmdj> implements ITkqKcxmdjService {

    @Resource
    private TkqKcxmdjMapper tkqKcxmdjMapper;
    @Resource
    private Cb08XzqhMapper cb08XzqhMapper;//行政区划

    @Override
    public Page<TkqKcxmdj> page(TkqKcxmdj tkqKcxmdj) {
        QueryWrapper<TkqKcxmdj> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public Page<TkqVo> getTkqVoPage(long current, long size, String nd, String xzqh, String bglx) {
        Page<TkqKcxmdj> page = new Page<>(current, size);
        QueryWrapper<TkqKcxmdj> queryWrapper = new QueryWrapper<>();
        if (nd != null) {
            queryWrapper.like("qfsj", nd);
        }
        if (xzqh != null) {
            xzqh = xzqh.replaceAll("0+$", "");
            queryWrapper.like("szxzq", xzqh);
        }

        if (bglx != null) {

        }
        page = tkqKcxmdjMapper.selectPage(page, queryWrapper);

        List<TkqKcxmdj> tkqKcxmdjs = page.getRecords();
        List<TkqVo> tkqVos = new ArrayList<>();
        Page<TkqVo> tkqVoPage = new Page<>();
        for (TkqKcxmdj tkqKcxmdj : tkqKcxmdjs) {
            TkqVo tkqVo = new TkqVo();
            BeanUtil.copyProperties(tkqKcxmdj, tkqVo);
            String xzqhdm = tkqKcxmdj.getSzxzq();

            if (xzqhdm != null && xzqhdm.length() > 0) {
                if (xzqhdm.contains("、")){
                    xzqhdm = xzqhdm.replaceAll("、", ",");
                }
                List<String> xzqhList = Arrays.asList(xzqhdm.split(","));
                String xianxzqh = "";

                if (xzqhList.size() > 0) {
                    for (String dm : xzqhList) {
                        //县
                        QueryWrapper<Cb08Xzqh> xianWrapper = new QueryWrapper<>();
                        xianWrapper.eq("dm", Integer.parseInt(dm));
                        Cb08Xzqh xian = cb08XzqhMapper.selectOne(xianWrapper);
                        if (xian != null){
                            xianxzqh += xian.getMc() + ",";
                        }

                    }
                    if (xianxzqh.length() > 0){
                        xianxzqh = xianxzqh.substring(0, xianxzqh.length() - 1);
                        tkqVo.setXianxzqhName(xianxzqh);
                    }

                    //县
                    QueryWrapper<Cb08Xzqh> xianWrapper = new QueryWrapper<>();
                    xianWrapper.eq("dm", Integer.parseInt(xzqhList.get(0)));
                    Cb08Xzqh xian = cb08XzqhMapper.selectOne(xianWrapper);
                    if (xian != null){
                        //市
                        QueryWrapper<Cb08Xzqh> shiWrapper = new QueryWrapper<>();
                        shiWrapper.eq("dm", xian.getFdm());
                        Cb08Xzqh shi = cb08XzqhMapper.selectOne(shiWrapper);
                        tkqVo.setShixzqhName(shi.getMc());
                        //省
                        QueryWrapper<Cb08Xzqh> shengWrapper = new QueryWrapper<>();
                        shengWrapper.eq("dm", shi.getFdm());
                        Cb08Xzqh sheng = cb08XzqhMapper.selectOne(shengWrapper);
                        tkqVo.setShengxzqhName(sheng.getMc());
                    }

                }
            }

            tkqVos.add(tkqVo);
        }

        tkqVoPage.setRecords(tkqVos);
        tkqVoPage.setCurrent(page.getCurrent());
        tkqVoPage.setSize(page.getSize());
        tkqVoPage.setTotal(page.getTotal());
        return tkqVoPage;
    }
}
